package com.hspedu.file_;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @author LiuJinTao
 * @className FileCreate
 * @date 2024/06/25 22:28
 * @description TODO
 */
public class FileCreate {
    public static void main(String[] args) {

    }

    // 方式一
    @Test
    public void create01() {
        String filePath = "e:\\news1.txt";
        File file = new File(filePath);

        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 方式二   根据 父目录 + 子路径
    @Test
    public void create02() {
        File parentFile = new File("e:\\");
        String fileName = "news2.txt";
        File file = new File(parentFile, fileName);

        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 方式三      根据 父路径 + 子路径
    @Test
    public void create03() {
        String parentPath = "e:\\";
        String childPath = "news3.txt";
        File file = new File(parentPath, childPath);

        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
